package com.yanqu.road.server.manager.peakstronghold;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.huashigang.HuaShiGangFloorConfig;
import com.yanqu.road.entity.activity.peakstronghold.config.PeakStrongHoldConfig;
import com.yanqu.road.entity.activity.peakstronghold.config.PeakStrongholdBaseConfig;
import com.yanqu.road.entity.activity.peakstronghold.config.PeakStrongholdDonateConfig;
import com.yanqu.road.entity.activity.peakstronghold.config.PeakStrongholdTimeConfig;

import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroup;
import com.yanqu.road.logic.bussiness.activity.PeakStrongholdActivityBusiness;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.pb.activity.StrongholdWarProto;
import com.yanqu.road.server.CrossBaseServer;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manager.activity.ActivityMgr;
import com.yanqu.road.server.manager.union.activitygroup.Cross2UnionActivityGroupMgr;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.date.DateHelper;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class CrossPeakStrongholdActivityMgr extends TempMgr {
    private static Map<Integer, PeakStrongHoldConfig> configs = new HashMap<>();

    private static RandomHelper randomHelper = new RandomHelper();
    private static Map<Integer, Long> reloadTimeMap = new ConcurrentHashMap<>();

    @Override
    public boolean reloadData() throws Exception {
        loadActivity();
        return true;
    }


    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }


    public static void loadActivity() {
        String nowTimeStr = DateHelper.getCurrentDateTimeString();
        List<Integer> activityTypeList = new ArrayList<>();
        activityTypeList.add(eActivityType.PeakStrongHold.getValue());
        Map<Integer, ActivityInfo> tempActivityInfoMap = ActivityBussiness.getOpenCrossRankActivityInfoMap(activityTypeList, nowTimeStr);
        for (ActivityInfo activityInfo : tempActivityInfoMap.values()) {
            Map<Integer, UnionActivityGroup> activityGroupMap = Cross2UnionActivityGroupMgr.getGroupMap(activityInfo.getActivityId());
            if (null != activityGroupMap) {
                for (UnionActivityGroup activityGroup : activityGroupMap.values()) {
                    if (activityGroup.getCrossServerId() == CrossBaseServer.getInstance().getServerId()) {
                        loadNewActivity(activityInfo);
                    }
                }
            }
        }
    }

    private static void loadNewActivity(ActivityInfo activityInfo) {
        int activityId = activityInfo.getActivityId();

        Map<Integer, HuaShiGangFloorConfig> tempDesertConfigMap = PeakStrongholdActivityBusiness.getPeakStrongholdDesertConfig(activityId);
        Map<Integer, PeakStrongholdTimeConfig> tempTimeConfigMap = PeakStrongholdActivityBusiness.getPeakStrongholdTimeConfig(activityId);
        Map<Integer, PeakStrongholdBaseConfig> tempBaseConfigMap = PeakStrongholdActivityBusiness.getPeakStrongholdBaseConfig(activityId);
        Map<Integer, PeakStrongholdDonateConfig> tempDonateConfigMap = PeakStrongholdActivityBusiness.getPeakStrongholdDonateConfig(activityId);

        PeakStrongHoldConfig configTemp = new PeakStrongHoldConfig();
        configTemp.setActivityInfo(activityInfo);
        Map<String, ActivityConfig> activityConfigMap = ActivityBussiness.getActivityConfigMap(Collections.singletonList(activityInfo.getActivityId())).get(activityInfo.getActivityId());
        configTemp.initConfig(activityConfigMap, tempDesertConfigMap, tempTimeConfigMap, tempBaseConfigMap, tempDonateConfigMap);
        configs.put(activityId, configTemp);

        CrossPeakStrongholdMgr.loadActivity(activityId);
    }

    public static ActivityInfo reloadActivityInfo(int activityId) {
        ActivityInfo activityInfo = ActivityBussiness.getActivityInfoByActivityId(activityId);
        if (ActivityMgr.activityInShowTime(activityInfo)) {
            loadNewActivity(activityInfo);
            return activityInfo;
        }
        return null;
    }

    public static void reloadActivityByGameServerNotify(StrongholdWarProto.CrossStrongholdReloadReqMsg reqMsg) {
        int activityId = reqMsg.getActivityId();
        if (reloadTimeMap.containsKey(activityId)) {
            return;
        }
        synchronized (reloadTimeMap) {
            if (reloadTimeMap.containsKey(activityId)) {
                return;
            }
            ActivityInfo openActivityInfo = CrossPeakStrongholdActivityMgr.getOpenActivityInfo(activityId);
            if (openActivityInfo != null &&
                    openActivityInfo.getBeginShowTime() == reqMsg.getStartShowTime() &&
                    openActivityInfo.getBeginTime() == reqMsg.getStartTime() &&
                    openActivityInfo.getEndTime() == reqMsg.getEndTime() &&
                    openActivityInfo.getEndShowTime() == reqMsg.getEndShowTime()) {
                return;
            }
            reloadActivityInfo(activityId);
            reloadTimeMap.remove(activityId);
        }
    }

    public static ActivityInfo getOpenActivityInfo(int activityId) {
        if (configs.containsKey(activityId)) {
            return configs.get(activityId).getActivityInfo();
        }
        return null;
    }

    public static Map<Integer, PeakStrongHoldConfig> getConfigs() {
        return configs;
    }

    public static PeakStrongHoldConfig getConfig(int activityId) {
        return configs.get(activityId);
    }

    public static int calcCurBlood(BigInteger curBlood, BigInteger totalBlood) {
        if (curBlood.compareTo(BigInteger.ZERO) <= 0) {
            return 0;
        }
        if (curBlood.equals(totalBlood)) {
            return 100;
        }
        int blood = new BigDecimal(curBlood).multiply(BigDecimal.valueOf(100)).divide(new BigDecimal(totalBlood), BigDecimal.ROUND_UP).intValue();
        if (blood == 100) {
            blood = 99;
        }
        return blood;
    }


    public static int calcEliminateCurBlood(BigInteger curBlood, BigInteger totalBlood) {
        if (curBlood.compareTo(BigInteger.ZERO) <= 0) {
            return 0;
        }
        if (curBlood.equals(totalBlood)) {
            return 10000;
        }
        int blood = new BigDecimal(curBlood).multiply(BigDecimal.valueOf(10000)).divide(new BigDecimal(totalBlood), BigDecimal.ROUND_UP).intValue();
        if (blood == 10000) {
            blood = 9999;
        }
        return blood;
    }


    public static int getCurTime(int activityId) {
        ActivityInfo activityInfo = getOpenActivityInfo(activityId);
        if (activityInfo == null) {
            return 0;
        }
        long beginTime = activityInfo.getBeginTime();
        long nowTime = System.currentTimeMillis() / 1000 - beginTime;
        int curTime = (int) nowTime / 3600;
        return curTime;
    }

    public static int getSortRandSeed() {
        return  randomHelper.next(Integer.MAX_VALUE);
    }
}
